package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.mapper.FarmerNutritionMapper;
import com.tz.farmer.model.FarmerKnowledge;
import com.tz.farmer.model.FarmerNutrition;
import com.tz.farmer.service.NutritionService;
import com.tz.farmer.so.BaseSo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NutritionServiceImpl extends ServiceImpl<FarmerNutritionMapper, FarmerNutrition> implements NutritionService {
    private static Logger logger = LoggerFactory.getLogger(NutritionServiceImpl.class);


    @Autowired
    FarmerNutritionMapper farmerNutritionMapper;

    @Override
    public ResultEx getNutritionInfo(BaseSo baseSo) throws Exception{
        //营养推荐信息
        LambdaQueryWrapper<FarmerNutrition> nutritionQueryWrapper = Wrappers.lambdaQuery();
        nutritionQueryWrapper.eq(FarmerNutrition::getProperty, baseSo.getFlag()).eq(FarmerNutrition::getIsSued,2).orderByDesc(FarmerNutrition::getReleaseTime);
        List<FarmerNutrition> farmerNutritions = farmerNutritionMapper.selectList(nutritionQueryWrapper);
        return ResultEx.builder().makeSuccessResult(farmerNutritions);
    }

    @Override
    public ResultEx getNutritionDetailInfo(FarmerNutrition farmerNutrition) throws Exception{
        //营养推荐详情信息
        LambdaQueryWrapper<FarmerNutrition> nutritionQueryWrapper = Wrappers.lambdaQuery();
        nutritionQueryWrapper.eq(FarmerNutrition::getId, farmerNutrition.getId());
        FarmerNutrition farmer = farmerNutritionMapper.selectOne(nutritionQueryWrapper);
        //更新后台查看次数
        FarmerNutrition nutrition=FarmerNutrition.builder().selectNum(farmer.getSelectNum()+1).id(farmerNutrition.getId()).build();
        farmerNutritionMapper.updateById(nutrition);
        logger.info("更新查看次数完成===============");
        return ResultEx.builder().makeSuccessResult(farmer);
    }
}
